package com.qingke.java.service.command;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.qingke.java.pojo.User;
import com.qingke.java.service.commandimpl.AddCommand;
import com.qingke.java.service.commandimpl.DeleteCommand;
import com.qingke.java.service.commandimpl.LogoutCommand;
import com.qingke.java.service.commandimpl.ResetCommand;
import com.qingke.java.service.commandimpl.UpdateCommand;

public class PlayerCommandFactory extends SystemCommandFactory {
	private User user;
	private static Map<CommandCode, Class<? extends Command>> cmdMap = new HashMap<>();
	public PlayerCommandFactory(User user) {
		this.user = user;
	}
	
	static {
		//ADD, DELETE, UPDATE, RESET, LOGOUT
		cmdMap.put(CommandCode.ADD, AddCommand.class);
		cmdMap.put(CommandCode.DELETE, DeleteCommand.class);
		cmdMap.put(CommandCode.UPDATE, UpdateCommand.class);
		cmdMap.put(CommandCode.RESET, ResetCommand.class);
		cmdMap.put(CommandCode.LOGOUT, LogoutCommand.class);
	}
	
	@Override
	public Command buildCommand(CommandCode cc) {
		Class<? extends Command> clazz = cmdMap.get(cc);
		if(clazz == null) {
			return super.buildCommand(cc);
		}
		
		try {
			Constructor<? extends Command> con = clazz.getConstructor(User.class);
			Command	command = con.newInstance(user);
			return command;
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch(InvocationTargetException e) {
			e.printStackTrace();
		} catch(IllegalArgumentException e) {
			e.printStackTrace();
		} catch(IllegalAccessException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
}
